package com.sky.controller.user;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description TODO
 * @Author itheima
 * @Date 2023-07-19 17:51
 */
@RestController
@Api(value = "购物车接口")
@RequestMapping("/user/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加商品到购物车相关接口
     * @param shoppingCartDTO
     * @return
     */
    @ApiOperation("添加商品到购物车相关接口")
    @PostMapping("/add")
    public Result  add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加商品到购物车相关接口:{}",shoppingCartDTO);
        shoppingCartService.add(shoppingCartDTO);
        return Result.success();
    }

    /**
     * 查看购物车数据
     * @return
     */
    @ApiOperation("查看购物车数据")
    @GetMapping("/list")
    public  Result<List<ShoppingCart>> list(){
        log.info("查看购物车数据");
        List<ShoppingCart> shoppingCarts = shoppingCartService.list();
        return Result.success(shoppingCarts);
    }

    /**
     * 清空购物车
     * @return
     */
    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public  Result clean(){
        log.info("清空购物车");
        shoppingCartService.clean();
        return Result.success();
    }

    /**
     * 删除购物车的商品
     * @param shoppingCartDTO
     * @return
     */
    @ApiOperation("删除购物车商品")
    @PostMapping ("/sub")
    public Result deletesub(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("删除商品:{}",shoppingCartDTO);
        shoppingCartService.deletesub(shoppingCartDTO);
        return Result.success();
    }

}
